<?php

header('Content-Type:text/html;charset=utf-8 ');

# http://projects:8081/cdapp-trex-api/api/print_exercise_content.php?
# http://projects:8081/cdapp-trex-api/api/print_exercise_content.php?offices=管制二室&year=2024

include_once './common.php';

$offices = isset($_GET['offices']) ? $_GET['offices'] : false;
$year = isset($_GET['year']) ? $_GET['year'] : false;
if (!$offices || !$year) {
  die('emm...');
}

$offices = explode(',', $offices);
$offices = implode("','", $offices);

$sql1 = "SELECT `number`, `title`, `year`, `cycle`, `type`, `office`, `subjects`, `hours` FROM `trex_exercises` WHERE `office` IN ('{$offices}') AND `year` = {$year} AND `valid` = 1 AND `disposed` = 0 ORDER BY `year`, `office`, `number`";
$result_exercise = exec_sql($sql1);

if (!$result_exercise) {
  die("missing result exercise<br/><br/>{$sql}");
}

$sql2 = "SELECT `content` FROM trex_params WHERE `name` = 'subjects'";
$result_subjects = exec_sql($sql2);
$subjects = json_decode($result_subjects[0]['content']);

$sql3 = "SELECT `content` FROM trex_params WHERE `name` = 'evaluations'";
$result_evaluations = exec_sql($sql3);
$evaluations = json_decode($result_evaluations[0]['content']);

echo '<!DOCTYPE html><html><head>';
echo '<title>TREX练习模板</title>';
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
echo '<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">';
echo '<style>*{}</style>';
echo '</head><body>';
echo '<h1>TREX练习模板</h1><div style="page-break-after:always;"></div>';
foreach ($result_exercise as $exercise) {
  // echo '<div style="padding-top:24px;"></div>';
  // echo '<div style="border-top:2px solid #aaa;margin-top:12px;padding-top:8px;">';
  echo '<div style="min-width:100%;padding:2em">';
  echo '<div style="padding-top:24px;"></div>';
  echo "<div style=\"text-align:center;font-size:24px;font-weight:bold;padding:6px 0;\">{$exercise['title']}</div>";
  echo "<div style=\"text-align:center;font-size:12px;\">{$exercise['office']} / {$exercise['year']}年度 / 第{$exercise['cycle']}轮 / {$exercise['hours']}学时</div>";
  echo '<div style="padding:18px;">';
  echo '<span style="padding-right:100px;border-bottom:1px solid #333;margin:0 12px;">教员：</span>';
  echo '<span style="padding-right:100px;border-bottom:1px solid #333;margin:0 12px;">学员：</span>';
  echo '<span style="margin:0 12px;">通过：&#9744;是&emsp;&#9744;否</span>';
  echo '</div>';
  echo '<div style="padding:18px;"></div>';
  echo '<div style="border-top:1px dashed #aaa"></div>';
  echo '<div style="padding:18px;"></div>';
  echo '<div style="width:100%;columns:2;">';
  echo '<table style="width:100%;text-align:justify;font-size:12px;">';
  # subjects
  $subjects_content = [];
  $subjects_list = json_decode($exercise['subjects']);
  foreach ($subjects_list as $subject_index => $subject_title) {
    $subject_number = $subject_index + 1;
    foreach ($subjects as $subject_category) {
      $category = $subject_category->label;
      foreach ($subject_category->children as $subject_children) {
        $children = $subject_children->label;
        $options = $subject_children->options;
        if ($subject_title == $children) {
          $subjects_content[] = ['category' => $category, 'title' => $children, 'options' => $options];
          echo "<tr style=\"font-weight:bold;\"><td width=\"12px\">{$subject_number}、</td><td>{$category}：$children</td></tr>";
          foreach ($options as $option_index => $option) {
            echo "<tr><td width=\"12px\">&#9744;</td><td>{$option}</td></tr>";
          }
          echo '<tr><td>&#9744;</td><td style="font-weight:bold;">本科目需针对性加练</td></tr>';
          echo '<tr style="height:6px;"></tr>';
        }

      }
    }
  }
  $evaluation_number = count($subjects_list) + 1;
  echo "<tr style=\"font-weight:bold;\"><td>{$evaluation_number}、</td><td>等级评定(A最好, E最差)：</td></tr>";
  foreach ($evaluations as $evaluation) {
    echo "<tr><td width=\"12px\">&#9733;</td><td>{$evaluation}：&#9744;A&emsp;&#9744;B&emsp;&#9744;C&emsp;&#9744;D&emsp;&#9744;E</td></tr>";
  }
  echo '</table></div>';
  // echo '<div style="padding-top:12px;font-size:12px;">*请在空白处记录评语</div>';
  echo '<div style="border-bottom:1px solid #000;padding-top:24px"></div>';
  echo '<div style="page-break-after:always;"></div></div>';
  
}
echo '</body></html>';
